import { base_url } from './config.js';
import { getToken } from '../util/tool.js';

export const circlePost = {
	// 上传动态图片接口
	async uploadCirclePostImg(img, circlePostId, idx) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/uploadCirclePostImg',
				method: 'POST',
				data: {
					img, circlePostId, idx
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request uploadCirclePostImg 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request uploadCirclePostImg 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 预发布动态接口
	async preAddCirclePost(title, publishTime, content, circlePostTypeId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/preAddCirclePost',
				method: 'POST',
				data: {
					title, publishTime, content, circlePostTypeId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request preAddCirclePost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request preAddCirclePost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 发布动态接口
	async addCirclePost(circlePostId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/addCirclePost',
				method: 'POST',
				data: {
					circlePostId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request addCirclePost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request addCirclePost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取动态详情接口
	async getPostInfo(postId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/getPostInfo?id=' + postId,
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getPostInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getPostInfo 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 删除动态
	async delPost(postId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/delPost',
				method: 'POST',
				data: {
					postId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request delPost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request delPost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 点赞动态接口
	async likePost(postId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/likePost',
				method: 'POST',
				data: {
					postId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request likePost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request likePost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 取消点赞接口
	async nolikePost(postId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/nolikePost',
				method: 'POST',
				data: {
					postId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request nolikePost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request nolikePost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 评论动态接口
	async addCommentPost(postId, time, content) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/addCommentPost',
				method: 'POST',
				data: {
					postId, time, content
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request addCommentPost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request addCommentPost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 删除评论
	async delCommentPost(postCommentId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/delCommentPost',
				method: 'POST',
				data: {
					postCommentId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request delCommentPost 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request delCommentPost 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 懒加载获取评论列表
	async getPostCommentList(postId, lastPostCommentId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/getPostCommentList',
				method: 'POST',
				data: {
					postId, lastPostCommentId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getPostCommentList 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getPostCommentList 】', err);
					reject({ success: false });
				}
			});
		});
	},
    // 获取推荐动态接口
	async getRecommendPostList(lastPostId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/getRecommendPostList',
				method: 'POST',
				data: {
					lastPostId
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getRecommendPostList 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getRecommendPostList 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取动态图片接口
	async getCirclePostImgUrl(imgId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/getCirclePostImgUrl?imgId=' + imgId,
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getCirclePostImgUrl 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getCirclePostImgUrl 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取所有动态类型接口
	async getAllCirclePostType() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/circlePost/getAllCirclePostType',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getAllCirclePostType 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getAllCirclePostType 】', err);
					reject({ success: false });
				}
			});
		});
	}
};